package com.wxw.BaseLibrary.http;

import android.content.ContentValues;
import android.database.Cursor;
import com.wxw.BaseLibrary.db.DBTool;
import com.wxw.BaseLibrary.infa.IDbObject;
import com.wxw.BaseLibrary.infa.IHttpCallback;
import com.wxw.BaseLibrary.infa.IJson;
import com.wxw.BaseLibrary.tool.SystemTool;
import com.wxw.BaseLibrary.tool.ToolsDate;


import java.util.ArrayList;

/**
 * Created by david on 2014/4/10.
 */
public class RequestMessage implements IDbObject{
    public static String TABLE_NAME = "SubmitTable";
    public int ID = 0;
    public String MessageName = "";
    public String RequestContent = "";
    public String ResponseContent = "";
    public String RequestService = "";
    public String RequestMethod = "";
    public String RequestParamName = "";
    public RequestMessageType MessageType = RequestMessageType.Download;
    public String MessageKey = "";
    public boolean Forever = false;
    public int CountDown = 0;
    public boolean Pause = false;
    public HTTP_METHOD httpMethod = HTTP_METHOD.GET;
    public RequestMessageStatus Status = RequestMessageStatus.Free;
    public String LastUpdate = "";

    public RequestMessage(){}

    public static RequestMessage insertRequestMessage(String messageName, String requestContent, String requestMethod, String requestParamName, String messageKey) {
        return insertRequestMessage(messageName, requestContent, RequestManager.DefaultRequestService, requestMethod, requestParamName, messageKey);
    }

    public static RequestMessage insertRequestMessage(String messageName, String requestContent, String requestService, String requestMethod, String requestParamName, String messageKey) {
        return insertRequestMessage(messageName, requestContent, requestService, requestMethod, requestParamName, messageKey, HTTP_METHOD.POST);
    }

    public static RequestMessage insertRequestMessage(String messageName, String requestContent, String requestService,
            String requestMethod, String requestParamName, String messageKey, HTTP_METHOD http_method) {
        return insertRequestMessage(messageName, requestContent, requestService, requestMethod, requestParamName,
                RequestMessageType.Download, messageKey, http_method);
    }

    public static RequestMessage insertRequestMessage(String messageName, String requestContent, String requestService,
            String requestMethod, String requestParamName, RequestMessageType messageType, String messageKey, HTTP_METHOD http_method) {
        RequestMessage message = new RequestMessage();
        message.MessageName = messageName;
        message.RequestContent = requestContent;
        message.RequestService = requestService;
        message.RequestMethod = requestMethod;
        message.RequestParamName = requestParamName;
        message.MessageType = messageType;
        message.MessageKey = messageKey;
        message.httpMethod = http_method;
        message.LastUpdate = ToolsDate.getDateLine();
        return insertRequestMessage(message);
    }

    public static RequestMessage insertRequestMessage(RequestMessage message) {
        message.insert(DBTool.getInstance());
        return message;
    }

    public static void initSubmitTable(DBTool dbTool) {
        dbTool.execSQL("Drop Table IF EXISTS " + TABLE_NAME);
        dbTool.execSQL("Create Table IF NOT EXISTS SubmitTable(ID INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "MessageName text, RequestContent text,ResponseContent text, RequestService text, "
                + "RequestMethod text, RequestParamName text, MessageType int, MessageKey text, Forever int, "
                + "CountDown int, Pause int, "
                + "httpMethod int, Status int, LastUpdate text)");
    }

    @Override
    public int insert(DBTool dbTool) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("MessageName", MessageName);
        contentValues.put("RequestContent", RequestContent);
        contentValues.put("ResponseContent", ResponseContent);
        contentValues.put("RequestService", RequestService);
        contentValues.put("RequestMethod", RequestMethod);
        contentValues.put("RequestParamName", RequestParamName);
        contentValues.put("MessageType", MessageType.getId());
        contentValues.put("MessageKey", MessageKey);
        contentValues.put("Forever", Forever);
        contentValues.put("CountDown", CountDown);
        contentValues.put("Pause", Pause);
        contentValues.put("httpMethod", httpMethod.ordinal());
        contentValues.put("Status", Status.ordinal());
        contentValues.put("LastUpdate", LastUpdate);
        int i = (int)dbTool.insert(TABLE_NAME, contentValues);
        if (i >= 0) {
            Cursor cursor = dbTool.query("Select ID From " + TABLE_NAME + " Where rowid=" + i);
            if (cursor != null && cursor.moveToNext()) {
                ID = cursor.getInt(0);
            }
        }
        return i;
    }

    @Override
    public int update(DBTool dbTool) {
        ContentValues contentValues = new ContentValues();
//        contentValues.put("MessageName", MessageName);
//        contentValues.put("RequestContent", RequestContent);
        contentValues.put("ResponseContent", ResponseContent);
//        contentValues.put("RequestService", RequestService);
//        contentValues.put("RequestMethod", RequestMethod);
//        contentValues.put("MessageKey", MessageKey);
        contentValues.put("Forever", Forever);
        contentValues.put("CountDown", CountDown);
        contentValues.put("Pause", Pause);
//        contentValues.put("httpMethod", httpMethod.ordinal());
        contentValues.put("Status", Status.ordinal());
        LastUpdate = ToolsDate.getDateLine();
        contentValues.put("LastUpdate", LastUpdate);
        return (int)dbTool.update(TABLE_NAME, contentValues, "ID = ?", new String[]{ String.valueOf(ID) });
    }

    @Override
    public int delete(DBTool dbTool) {
        String where = "ID = ?";
        String[] where_value = new String[] { String.valueOf(ID) };
        return dbTool.delete(TABLE_NAME, where, where_value);
    }

    @Override
    public IDbObject full(Cursor cursor) {
        if (cursor != null && cursor.moveToNext()) {
            RequestMessage message = new RequestMessage();
            message.ID = cursor.getInt(0);
            message.MessageName = cursor.getString(1);
            message.RequestContent = cursor.getString(2);
            message.ResponseContent = cursor.getString(3);
            message.RequestService = cursor.getString(4);
            message.RequestMethod = cursor.getString(5);
            message.RequestParamName = cursor.getString(6);
            message.MessageType = RequestMessageType.getInstance(cursor.getInt(7));
            message.MessageKey = cursor.getString(8);
            message.Forever = cursor.getInt(9)==0?false:true;
            message.CountDown = cursor.getInt(10);
            message.Pause = cursor.getInt(11)==0?false:true;
            message.httpMethod = HTTP_METHOD.values()[cursor.getInt(12)];
            message.Status = RequestMessageStatus.values()[cursor.getInt(13)];
            message.LastUpdate = cursor.getString(14);
            return message;
        }
        return null;
    }

    @Override
    public void clear(DBTool dbTool) {

    }

    @Override
    public String getDataSql() {
        return null;
    }

    @Override
    public String getSyncMethod() {
        return null;
    }

    @Override
    public boolean doClear() {
        return false;
    }

    @Override
    public String toString() {
        return MessageName;
    }

    public static String getHttpResponseContent(String s) {
        try {
            int start = s.indexOf("<ns:return>") + 11;
            int end = s.indexOf("</ns:return>");
            return s.substring(start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return s;
        }
    }

    public enum HTTP_METHOD{
        DELETE,
        GET,
        POST,
        PUT
    }
}
